<?php

namespace App\Http\Controllers\Admin\Cashier;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Cashier\Member;
use App\Models\Cashier\Store;

class MemberController extends Controller
{
    public function __construct()
    {
        view()->share([
            '_cashier' => 'active open',
            '_member' => 'active',
            'stores' => Store::orderBy('id', 'desc')->get()
        ]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where = function ($query) use ($request) {
            //按商品名称
            if ($request->has('keyword') and $request->keyword != '') {
                $search = "%" . $request->keyword . "%";
                $query->where('name', 'like', $search);
            }

            //按电话
            if ($request->has('key') and $request->key != '') {
                $search = "%" . $request->key . "%";
                $query->where('phone', 'like', $search);
            }

            //按品牌
            if ($request->has('store_id') and $request->store_id != '-1') {
                $query->where('store_id', $request->store_id);
            }

            //按照时间搜索
            if ($request->has('created_at') and $request->created_at != '') {
                $time = explode(" ~ ", $request->input('created_at'));
                $start_time = $time[0] . ' 00:00:00'; //这里单引号前面要打一空格
                $end_time = $time[1] . ' 23:59:59';
                $query->whereBetween('created_at', [$start_time, $end_time]);
            }
        };
        $members = Member::with('store')->where($where)->orderBy('created_at', 'desc')->paginate(env('pageSize'));
//        return $members;
        return view('admin.cashier.member.index', compact('members'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.cashier.member.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|unique:customers|max:255',
            'phone' => 'required|regex:/^1[34578][0-9]{9}$/',
            'birthday' => 'required',
            'manager' => 'required',
            'store_id' => 'required',
        ]);

        Member::create($request->all());
        return redirect(route('cashier.member.index'))->with('success', '新增会员成功~');

    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $member = Member::find($id);
        return view('admin.cashier.member.edit', compact('member'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'name' => 'required|max:255',
            'phone' => 'required|regex:/^1[34578][0-9]{9}$/',
            'birthday' => 'required',
            'manager' => 'required',
            'store_id' => 'required',
        ]);

        $member = Member::find($id);
        $member->update($request->all());
        return redirect(route('cashier.member.index'))->with('success', '编辑会员信息成功~');
    }

    /**
     * ajax 删除单条
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Member::destroy($id);
    }
}
